﻿using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PcProject.Infrastructure.Repository
{
    public class BargainRepository: DapperRepositoryBase<Bargain>, IBargainRepository
    {
        //构造函数依赖注入
        public IConfiguration _configuration;
        //函数依赖注入
        public BargainRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }


        /// <summary>
        /// 砍价显示查询分页
        /// </summary>
        /// <returns></returns>
        public List<BargainDto> GetBargainAsync()
        {
            string sql = $"select * from Bargain a left join Product b on a.Pid=b.ProId left join LaunchBargain c on a.BargainId=c.ActiveId left join Users d on c.Uid=d.UserId";
            return Conn.Query<BargainDto>(sql).ToList();
        }

        /// <summary>
        /// 砍价添加
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> InserAsync(Bargain obj)
        {
            return (int)await Conn.InsertAsync(obj);
        }

        /// <summary>
        /// 砍价删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> DeleAsync(int id)
        {
            string sql = $"delete from Bargain where BargainId={id}";
            return await Conn.ExecuteAsync(sql);
        }

        /// <summary>
        /// 砍价反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<Bargain> FillAsync(int id)
        {
            return await Conn.GetAsync<Bargain>(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public BargainDto Fill(int id)
        {
            string sql = $"select * from Bargain a left join LaunchBargain b on a.BargainId=b.ActiveId left join Product c on a.Pid=c.ProId where BargainId={id}";
            return Conn.Query<BargainDto>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 砍价修改
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> UptAsync(Bargain obj)
        {
            return (int)await Conn.UpdateAsync(obj);
        }

        /// <summary>
        /// 修改价格
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int Upt(LaunchBargain obj)
        {
            return Conn.Update(obj);
        }

    }
}
